Namespaces
Variants

std::literals::chrono_literals:: operator""y

From cppreference.net
定义于头文件 <chrono>
constexpr std:: chrono :: year operator "" y ( unsigned long long y ) noexcept ;
(C++20 起)

形成一个表示 公历推前历法 中年份的 std::chrono::year 字面量。

目录

参数

y - 年份值

返回值

一个从 int ( y ) 初始化的 std::chrono::year 。如果 y 不在范围 [ - 32767 , 32767 ] 内,则存储的值是未指定的。

可能的实现

constexpr std::chrono::year operator""y(unsigned long long y) noexcept
{
    return std::chrono::year(static_cast<int>(y));
}

注释

此运算符声明于命名空间 std :: literals :: chrono_literals 中,其中 literals chrono_literals 均为 内联命名空间 。可通过以下方式访问此运算符:

  • using namespace std :: literals
  • using namespace std :: chrono_literals ,或
  • using namespace std :: literals :: chrono_literals

此外,在命名空间 std :: chrono 中, 标准库 提供了指令 using namespace literals :: chrono_literals ; 。因此,若程序员使用 using namespace std :: chrono ; 来访问 chrono 库 中的类,对应的字面量运算符也会同时可见。

示例

#include <chrono>
#include <iostream>
int main()
{
    using namespace std::literals;
    std::cout << int(2020y)  << '\t' << 2020y  << '\n'
              << int(-220y)  << '\t' << -220y  << '\n'
              << int(3000y)  << '\t' << 3000y  << '\n'
              << int(32768y) << '\t' << 32768y << '\n'  // 未指定
              << int(65578y) << '\t' << 65578y << '\n'; // 未指定
}

可能的输出:

2020	2020
-220	-0220
3000	3000
-32768	-32768 is not a valid year
42	0042

参见

构造一个 year 对象
( std::chrono::year 的公开成员函数)